package com.cutefool.zero.code.admin.dbms.support;

import com.cutefool.rds.enums.RdsDbType;
import com.cutefool.rds.template.RdsTemplate;
import com.cutefool.zero.code.admin.meta.model.context.RdsTemplateContext;
import com.cutefool.zero.code.core.domain.Db;
import com.zaxxer.hikari.HikariDataSource;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class DbmsExecutor {

    @Resource
    private RdsTemplateContext rdsTemplateContext;

    /**
     * 初始化数据库函数
     *
     * @param db
     */
    public void doInitFunction(Db db) {
        RdsDbType.byName(db.getType())
                .ifPresent(s -> {
                    RdsTemplate rdsTemplate = rdsTemplateContext.get(db);
                    s.getFunctioned().drop(rdsTemplate.getJdbcTemplate());
                    s.getFunctioned().doExecute((HikariDataSource) rdsTemplate.getJdbcTemplate().getDataSource());
                });
    }



}
